<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>随机点名工具</title>
  <style>
    body {
      font-family: 'Microsoft YaHei', sans-serif;
      max-width: 800px;
      margin: 0 auto;
      padding: 20px;
      text-align: center;
      background-color: #f5f5f5;
    }
    h1 {
      color: #333;
    }
    textarea {
      width: 100%;
      height: 150px;
      margin: 15px 0;
      padding: 10px;
      border: 1px solid #ddd;
      border-radius: 4px;
      resize: vertical;
    }
    .control-panel {
      margin: 20px 0;
      padding: 15px;
      background-color: #fff;
      border-radius: 8px;
      box-shadow: 0 2px 5px rgba(0,0,0,0.1);
    }
    button {
      background-color: #4CAF50;
      color: white;
      border: none;
      padding: 10px 20px;
      text-align: center;
      text-decoration: none;
      display: inline-block;
      font-size: 16px;
      margin: 10px 5px;
      cursor: pointer;
      border-radius: 4px;
      transition: background-color 0.3s;
    }
    button:hover {
      background-color: #45a049;
    }
    #result {
      margin: 20px 0;
      padding: 15px;
      background-color: #fff;
      border-radius: 8px;
      min-height: 100px;
      box-shadow: 0 2px 5px rgba(0,0,0,0.1);
      font-size: 24px;
      color: #e74c3c;
    }
    .name-item {
      display: inline-block;
      margin: 5px;
      padding: 8px 15px;
      background-color: #3498db;
      color: white;
      border-radius: 20px;
      animation: pop 0.5s;
    }
    @keyframes pop {
      0% { transform: scale(0.8); opacity: 0; }
      50% { transform: scale(1.1); }
      100% { transform: scale(1); opacity: 1; }
    }
    .count-input {
      width: 60px;
      padding: 8px;
      text-align: center;
      border: 1px solid #ddd;
      border-radius: 4px;
    }
    .highlight {
      animation: highlight 1s;
    }
    @keyframes highlight {
      0% { background-color: #ffeb3b; }
      100% { background-color: #fff; }
    }
  </style>
</head>
<body>
<h1>随机点名工具</h1>

<div class="control-panel">
  <h3>名单输入</h3>
  <textarea id="nameList" placeholder="请输入名单，每行一个名字"></textarea>

  <div>
    <label for="count">随机数量：</label>
    <input type="number" id="count" class="count-input" min="1" value="1">
    <button id="randomBtn">开始随机</button>
    <button id="resetBtn">重置</button>
  </div>
</div>

<div id="result">结果将显示在这里...</div>

<script>
  document.addEventListener('DOMContentLoaded', function() {
    const nameListTextarea = document.getElementById('nameList');
    const randomBtn = document.getElementById('randomBtn');
    const resetBtn = document.getElementById('resetBtn');
    const resultDiv = document.getElementById('result');
    const countInput = document.getElementById('count');

    // 示例名单
    nameListTextarea.value = "张三\n李四\n王五\n赵六\n钱七\n孙八\n周九\n吴十";

    randomBtn.addEventListener('click', function() {
      const namesText = nameListTextarea.value.trim();
      if (!namesText) {
        alert('请输入名单！');
        return;
      }

      const names = namesText.split('\n').filter(name => name.trim() !== '');
      if (names.length === 0) {
        alert('名单不能为空！');
        return;
      }

      let count = parseInt(countInput.value);
      if (isNaN(count) || count < 1) {
        count = 1;
        countInput.value = '1';
      }

      if (count > names.length) {
        count = names.length;
        countInput.value = count.toString();
      }

      // 随机选择名字
      const selectedNames = [];
      const availableNames = [...names];

      for (let i = 0; i < count; i++) {
        if (availableNames.length === 0) break;

        const randomIndex = Math.floor(Math.random() * availableNames.length);
        selectedNames.push(availableNames[randomIndex]);
        availableNames.splice(randomIndex, 1);
      }

      // 显示结果
      displayResult(selectedNames);
    });

    resetBtn.addEventListener('click', function() {
      nameListTextarea.value = "";
      resultDiv.innerHTML = "结果将显示在这里...";
      countInput.value = "1";
    });

    function displayResult(names) {
      resultDiv.innerHTML = '';
      resultDiv.classList.add('highlight');

      setTimeout(() => {
        resultDiv.classList.remove('highlight');
      }, 1000);

      if (names.length === 1) {
        const nameElement = document.createElement('div');
        nameElement.textContent = names[0];
        nameElement.style.fontSize = '48px';
        nameElement.style.fontWeight = 'bold';
        nameElement.style.color = '#e74c3c';
        nameElement.style.margin = '20px 0';
        resultDiv.appendChild(nameElement);
      } else {
        const title = document.createElement('div');
        title.textContent = `随机选出 ${names.length} 人：`;
        title.style.marginBottom = '15px';
        title.style.fontSize = '20px';
        resultDiv.appendChild(title);

        names.forEach(name => {
          const nameElement = document.createElement('span');
          nameElement.textContent = name;
          nameElement.className = 'name-item';
          resultDiv.appendChild(nameElement);
        });
      }
    }
  });
</script>
</body>
</html>